package com.tj.collection.controller.lending.response;

import com.fqgj.common.api.ResponseData;
import com.fqgj.common.utils.CollectionUtils;
import com.tj.collection.service.dto.CollectionOverdueStatementVo;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CollectionOverdueStatementResponse implements ResponseData {

    List<Item> result = new ArrayList<>();

    public List<Item> getResult() {
        return result;
    }

    public void setResult(List<Item> result) {
        this.result = result;
    }

    public CollectionOverdueStatementResponse(List<CollectionOverdueStatementVo> collectionOverdueStatementVos) {

        if (CollectionUtils.isEmpty(collectionOverdueStatementVos)) {
            return;
        }

        result = collectionOverdueStatementVos
                .parallelStream()
                .map(vo -> {
                    return new Item()
                            .setRepaymentDate(vo.getRepaymentDate())
                            .setNumberOfRepayment(vo.getNumberOfRepayment())
                            .setNumberOfPayOff(vo.getNumberOfPayOff())
                            .setNumberOfOverdue(vo.getNumberOfOverdue())
                            .setPayOffRate(format(vo.getNumberOfPayOff(), vo.getNumberOfRepayment()))
                            .setOverdue0Rate(format(vo.getNumberOfOverdue0(), vo.getNumberOfRepayment()))
                            .setOverdue7Rate(format(vo.getNumberOfOverdue7(), vo.getNumberOfRepayment()))
                            .setOverdue15Rate(format(vo.getNumberOfOverdue15(), vo.getNumberOfRepayment()))
                            .setOverdue20Rate(format(vo.getNumberOfOverdue20(), vo.getNumberOfRepayment()))
                            .setNowOverdueRate(format(vo.getNumberOfOverdueNow(), vo.getNumberOfRepayment()))
                            .setPaidIn1T7Rate(format(vo.getPaidIn1T7(), vo.getNumberOfRepayment()))
                            .setPaidIn8T15Rate(format(vo.getPaidIn8T15(), vo.getNumberOfRepayment()))
                            .setPaidIn16T20Rate(format(vo.getPaidIn16T20(), vo.getNumberOfRepayment()));
                })
                .collect(Collectors.toList());
    }

    /**
     * @param dividend 被除数
     * @param divisor  除数
     * @return
     */
    public String format(Integer dividend, Integer divisor) {
        if (divisor == 0 || divisor == null) {
            divisor = 1;
        }
        DecimalFormat df = new DecimalFormat("0.00");//保留2位小数
        Float res = new BigDecimal((float) dividend / divisor).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue() * 100;
        return df.format(res) + "%";
    }

    private class Item {

        private String repaymentDate; //账单应还日期
        private Integer numberOfRepayment;   //应还账单
        private Integer numberOfPayOff;  //还清账单
        private Integer numberOfOverdue; //逾期账单 Overdue Count
        private String payOffRate;    //还清比率
        private String overdue0Rate;  //未逾期的
        private String overdue7Rate;  //逾期7天的
        private String overdue15Rate;  //逾期15天的
        private String overdue20Rate;  //逾期20天的
        private String nowOverdueRate; //到当前时间还逾期的
        private String paidIn1T7Rate; //1到7天还的比率
        private String paidIn8T15Rate; //8到15天还的比率
        private String paidIn16T20Rate; //16到20天还的比率

        public String getRepaymentDate() {
            return repaymentDate;
        }

        public Item setRepaymentDate(String repaymentDate) {
            this.repaymentDate = repaymentDate;
            return this;
        }

        public Integer getNumberOfRepayment() {
            return numberOfRepayment;
        }

        public Item setNumberOfRepayment(Integer numberOfRepayment) {
            this.numberOfRepayment = numberOfRepayment;
            return this;
        }

        public Integer getNumberOfPayOff() {
            return numberOfPayOff;
        }

        public Item setNumberOfPayOff(Integer numberOfPayOff) {
            this.numberOfPayOff = numberOfPayOff;
            return this;
        }

        public String getPayOffRate() {
            return payOffRate;
        }

        public Item setPayOffRate(String payOffRate) {
            this.payOffRate = payOffRate;
            return this;
        }

        public Integer getNumberOfOverdue() {
            return numberOfOverdue;
        }

        public Item setNumberOfOverdue(Integer numberOfOverdue) {
            this.numberOfOverdue = numberOfOverdue;
            return this;
        }

        public String getOverdue0Rate() {
            return overdue0Rate;
        }

        public Item setOverdue0Rate(String overdue0Rate) {
            this.overdue0Rate = overdue0Rate;
            return this;
        }

        public String getOverdue7Rate() {
            return overdue7Rate;
        }

        public Item setOverdue7Rate(String overdue7Rate) {
            this.overdue7Rate = overdue7Rate;
            return this;
        }

        public String getOverdue15Rate() {
            return overdue15Rate;
        }

        public Item setOverdue15Rate(String overdue15Rate) {
            this.overdue15Rate = overdue15Rate;
            return this;
        }

        public String getOverdue20Rate() {
            return overdue20Rate;
        }

        public Item setOverdue20Rate(String overdue20Rate) {
            this.overdue20Rate = overdue20Rate;
            return this;
        }

        public String getNowOverdueRate() {
            return nowOverdueRate;
        }

        public Item setNowOverdueRate(String nowOverdueRate) {
            this.nowOverdueRate = nowOverdueRate;
            return this;
        }

        public String getPaidIn1T7Rate() {
            return paidIn1T7Rate;
        }

        public Item setPaidIn1T7Rate(String paidIn1T7Rate) {
            this.paidIn1T7Rate = paidIn1T7Rate;
            return this;
        }

        public String getPaidIn8T15Rate() {
            return paidIn8T15Rate;
        }

        public Item setPaidIn8T15Rate(String paidIn8T15Rate) {
            this.paidIn8T15Rate = paidIn8T15Rate;
            return this;
        }

        public String getPaidIn16T20Rate() {
            return paidIn16T20Rate;
        }

        public Item setPaidIn16T20Rate(String paidIn16T20Rate) {
            this.paidIn16T20Rate = paidIn16T20Rate;
            return this;
        }
    }

}
